
package com.example.testutil.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ReflectUtils {

    private ReflectUtils() {
        throw new UnsupportedOperationException("cannot be instantiated");
    }

    
    /*
     * 获取类
     */
    public static Class<?> getClazz(String className) {
        Class<?> clazz = null;
        try {
            clazz = Class.forName(className);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return clazz;
    }

    
    /*
     * 获取某个类的实例对象
     */
    public static Object newInstance(Class clazz, Object... constructorArags) {
        if (clazz == null) {
            return null;
        }
        Object object = null;
        int argsLenght = constructorArags == null ? 0 : constructorArags.length;
        Class<?>[] paramterTypes = new Class[argsLenght];
        for (int i = 0; i < argsLenght; i++) {
            paramterTypes[i] = constructorArags[i].getClass();
        }
        try {
            Constructor constructor = clazz.getDeclaredConstructor(paramterTypes);
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            object = constructor.newInstance(constructorArags);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }

    
    /*
     * 反射调用方法
     */
    public static Object invokeMethod(Object object, String methodName, Object... objects) {
        if (object == null) {
            return null;
        }
        Object result = null;
        Class<?> clazz = object.getClass();
        try {
            Method method = clazz.getDeclaredMethod(methodName, obj2class(objects));
            if (method != null) {
                if (!method.isAccessible()) {
                    method.setAccessible(true);
                }
                result = method.invoke(object, objects);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    
    
    /*
     * 反射调用获取属性值
     */
    public static Object getField(Object object, String fieldName) {
        if (object == null) {
            return null;
        }
        Object result = null;
        Class<?> clazz = object.getClass();
        try {
            Field field = clazz.getDeclaredField(fieldName);
            if (field != null) {
                if (field.isAccessible()) {
                    field.setAccessible(true);
                    result = field.get(object);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /*
     * 反射获取属性 并设置
     */
    public static boolean setField(Object object,String fieldname,Object value) {
        if(object == null) {
            return false;
        }
        Class<?> clazz = object.getClass();
        try {
            Field field = clazz.getDeclaredField(fieldname);
            if(field != null) {
                if(!field.isAccessible()) {
                    field.setAccessible(true);
                    field.set(object, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /*
     * 反射调用静态方法
     */
    public static Object invokeStaticMethod(Class<?> clazz,String methodname,Object...objects) {
        Object result = null;
        int argsLenght = objects == null ? 0 : objects.length;
        Class<?>[] paramterTypes = new Class[argsLenght];
        for (int i = 0; i < argsLenght; i++) {
            paramterTypes[i] = objects[i].getClass();
        }
        try {
            Method method = clazz.getDeclaredMethod(methodname, paramterTypes);
            result = method.invoke(null, method);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /*
     * 反射获取静态属性值
     */
    public static Object getStaticField(Class<?> clazz,String FieldName) {
        Object result =null;
        try {
            Field field = clazz.getDeclaredField(FieldName);
            result = field.get(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
    /*
     * 反射设置静态属性值
     */
    public static boolean setStaticField(Class<?> clazz,String fieldName,Object values) {
        try {
            Field field = clazz.getDeclaredField(fieldName);
            field.set(null, values);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
    /*
     * object to class
     */
    public static Class<?>[] obj2class(Object... objects) {
        int objargs = objects.length;
        Class<?>[] classes = new Class[objargs];
        for (int i = 0; i < objargs; i++) {
            classes[i] = objects[i].getClass();
        }
        return classes;
    }
}
